//object 转 stirng
function obj2str(o) {
	var r = [];
	if (typeof o == "string" || o == null) {
		return o;
	}
	if (typeof o == "object") {
		if (!o.sort) {
			r[0] = "{"
			for (var i in o) {
				r[r.length] = i;
				r[r.length] = ":";
				r[r.length] = obj2str(o[i]);
				r[r.length] = ",";
			}
			r[r.length - 1] = "}"
		} else {
			r[0] = "["
			for (var i = 0; i < o.length; i++) {
				r[r.length] = obj2str(o[i]);
				r[r.length] = ",";
			}
			r[r.length - 1] = "]"
		}
		return r.join("");
	}
	return o.toString();
}

//+---------------------------------------------------   
//| 返回下载URL 
//  格式：/marker/110516/phone/1122037753.xls(刘文辉)
//+---------------------------------------------------
function toDownloadUrl(str){
	var newstr ="";
     if(str.length>0){
     	 newstr ="./download.down?homeid=upDir";
     	 if(str.indexOf("/")>-1){
     	 	 str =str.substring(str.indexOf("/")+1,str.length);
     	 	 var model =str.substring(0,str.indexOf("/"));
     	 	 newstr += "&model="+model;
     	 }else{
     	     newstr ="";
     	 }

     	 if(str.indexOf("/")>-1){
     	 	 str =str.substring(str.indexOf("/")+1,str.length);
     	 	 var date =str.substring(0,str.indexOf("/"));
     	 	 newstr += "&date="+date;
     	 }else{
     	     newstr ="";
     	 }
     	 
     	 if(str.indexOf("/")>-1){
     	 	 str =str.substring(str.indexOf("/")+1,str.length);
     	 	 var type =str.substring(0,str.indexOf("/"));
     	 	 newstr += "&type="+type;
     	 }else{
     	     newstr ="";
     	 }
     	 
     	 if(str.indexOf("/")>-1){
     	 	 str =str.substring(str.indexOf("/")+1,str.length);
     	 	 newstr += "&fn="+str;
     	 }else{
     	     newstr ="";
     	 }
     }
     return newstr;
}
//string 截取
function strsubjson(str){
    return str.substring(str.indexOf("{"),str.indexOf("}")+1);
}

//string 转 object
function strToObj(json) {
	return eval("(" + json + ")");
}

//+---------------------------------------------------   
//| 获取字符串字节数  (刘文辉)
//+---------------------------------------------------
function getBytesCount(str){
    if (str == null){
        return 0;    
    }else{    
        return (str.length + str.replace(/[\u0000-\u00ff]/g," ").length);    
    }    
}
//+---------------------------------------------------   
//| 换算大小
//+---------------------------------------------------
function fSizeJs(size){
	 if(size<=1024){
	     return 1;
	 }else{
	     if(size % 1024>1){
	         return parseInt(size/1024)+1;
	     }else{
	         return size/1024;
	     }
	 }
}
//+---------------------------------------------------   
//| 字符串全部替换 (刘文辉)
//+---------------------------------------------------
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {   
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {   
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);   
    } else {   
        return this.replace(reallyDo, replaceWith);   
    }   
}     
   //************************ bzq add ***********************
  /**机构匹配功能全局变量*/
  var ecorder_grid_id = '';

  function zTreeOnBeforeClick(treeId, treeNode) {
		var check = (treeNode && !treeNode.isParent);
		if (!check) return check;
	}

  /**
	* 显示文本框的字数
	*/
	function checkMaxInput(ta, maxLen,div_)
	{
	 	 
		if (ta.value.length > maxLen)
		{
				ta.value = ta.value.substring(0, maxLen);
		}
		var left = maxLen - ta.value.length;
		document.getElementById(div_).innerHTML="您最多可输入"+maxLen+"个字; 您现在输入了<font color=red>"+ta.value.length+"</font>个字;<br> 还可以输入<font color=red>"+left+"</font>个字.";
	}
	
	//判断结束时间是否大于开始时间
	//结束时间>开始时间  返回 false
	function comptime(beginTime,endTime){ 
		 var beginTimes=beginTime.substring(0,10).split('-');
		 var endTimes=endTime.substring(0,10).split('-');
		 
		 beginTime=beginTimes[1]+'/'+beginTimes[2]+'/'+beginTimes[0]+' '+beginTime.substring(10,19);
		 endTime=endTimes[1]+'/'+endTimes[2]+'/'+endTimes[0]+' '+endTime.substring(10,19);
		
		 var a =(Date.parse(endTime)-Date.parse(beginTime))/3600/1000;
		 
		 if(a<0){
		   timeformat=true;
		 }else{
		   timeformat=false;
		 }
		 return timeformat;
	}

	//下拉框动态生成
//selectName:下拉框id
//json:json格式串
//text:json格式串显示于option中text的属性名
//value:json格式串显示于option中value的属性名
//修改人:csh,修改时间:2011年3月23日
//修改时间:下拉框不要有dataRows
function addoptions(selectName,json,text,value){
		for(var i=0;i<json.length;i++){
   			$('#'+selectName).append("<option value='"+json[i][value]+"'>"+json[i][text]+"</option>");
   		}
}

function removeoptions(selectName,json,value){
		for(var i=0;i<json.length;i++){
			$('#'+selectName+' option[value='+json[i][value]+']').remove(); 
   		}
}

/*
*
*   判断在数组中是否含有给定的一个变量值
*   参数：
*   needle：需要查询的值
*   haystack：被查询的数组
*   在haystack中查询needle是否存在，如果找到返回true，否则返回false。
*   此函数只能对字符和数字有效
*
*/
function in_array(needle, haystack) {

	var type = typeof needle;
	if (type == 'string' || type == 'number') {
		for (var i in haystack) {
			if (haystack[i] == needle) {
				return true;
			}
		}
	}
	return false;
}

function closeWindow() {   
  window.open('','_parent','');   
  window.close();   
}

jQuery.extend(   
{   
   /**  
   * @see   将json字符串转换为对象  
   * @param   json字符串  
   * @return 返回object,array,string等对象  
   */  
   evalJSON : function (strJson)   
   {   
    return eval( "(" + strJson + ")");   
   }   
});   
jQuery.extend(   
{   
   /**  
   * @see   将javascript数据类型转换为json字符串  
   * @param 待转换对象,支持object,array,string,function,number,boolean,regexp  
   * @return 返回json字符串  
   */  
   toJSON : function (object)   
   {   
    var type = typeof object;   
    if ('object' == type)   
    {   
     if (Array == object.constructor)   
      type = 'array';   
     else if (RegExp == object.constructor)   
      type = 'regexp';   
     else  
      type = 'object';   
    }   
      switch(type)   
    {   
        case 'undefined':   
       case 'unknown':    
      return;   
      break;   
     case 'function':   
       case 'boolean':   
     case 'regexp':   
      return object.toString();   
      break;   
     case 'number':   
      return isFinite(object) ? object.toString() : 'null';   
        break;   
     case 'string':   
      return '"' + object.replace(/(\\|\")/g,"\\$1").replace(/\n|\r|\t/g,   
        function(){      
                var a = arguments[0];                      
         return (a == '\n') ? '\\n':      
                       (a == '\r') ? '\\r':      
                       (a == '\t') ? '\\t': ""    
            }) + '"';   
      break;   
     case 'object':   
      if (object === null) return 'null';   
        var results = [];   
        for (var property in object) {   
          var value = jQuery.toJSON(object[property]);   
          if (value !== undefined)   
            results.push(jQuery.toJSON(property) + ':' + value);   
        }   
        return '{' + results.join(',') + '}';   
      break;   
     case 'array':   
      var results = [];   
        for(var i = 0; i < object.length; i++)   
      {   
       var value = jQuery.toJSON(object[i]);   
          if (value !== undefined) results.push(value);   
      }   
        return '[' + results.join(',') + ']';   
      break;   
      }   
   }   
});

//+---------------------------------------------------   
//| 自动滚动 (刘文辉)
//| 停车时间 timer
//| 播放时间 pause
//| 是否连续播放 ip 
//+---------------------------------------------------
$.fn.autoScroll = function(timer,pause,ip) {
				      var obj=$(this);
				      function _scroll(){
		                   var pos =obj.scrollTop();
		                   pos++;
		                   obj.scrollTop(++pos);
		                   if (pos > obj.scrollTop()){
		                   	   if(!ip){
		                   	   	   return;
		                   	   }
							   autoStop();
							   setTimeout(function(){
							    	obj.scrollTop(0); 
									autoPlay();
								},pause);
		                     }
		              }
				      var autoPlay = function(){
				      	  if(timer)timerID = window.setInterval(_scroll,timer);
				      };
				      var autoStop = function(){
				          if(timer)window.clearInterval(timerID);
				      };
				      obj.hover(autoStop,autoPlay).mouseout();
				  }